package com.ikangtai.shecare.record;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.signature.StringSignature;
import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.eventBusMsg.RecordInfo;
import com.ikangtai.shecare.common.util.AppLog;

import java.io.File;
import java.util.List;

public class EarlyPregnancyPaperPhotoAdapter extends BaseAdapter {

    private List<RecordInfo> list;
    private Context mContext;

    public EarlyPregnancyPaperPhotoAdapter(Context context, List<RecordInfo> list) {
        this.mContext = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.view_ovu_paper_photo_adapter, null);
            viewHolder = new ViewHolder();
            viewHolder.textViewLeft = (TextView) convertView.findViewById(R.id.record_ovu_photo_left_text);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.record_ovu_photo_middle);
            viewHolder.textViewRight = (TextView) convertView.findViewById(R.id.record_ovu_photo_right_text);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        }
        if (list.size() > 0) {
            if (list.get(position).getHCGIsSynced() == 1) {
                Glide.clear(viewHolder.imageView);
                String picURL = App.gynaeUrlPath + "hcgpaper/" + App.userName + "/" + list.get(position).getHCGDate().substring(0, 10) + "/" + list.get(position).getHCGPaperID() + ".jpg";
                Glide.with(mContext).load(picURL).signature(new StringSignature("1")).into(viewHolder.imageView);
            } else if (list.get(position).getHCGIsSynced() == 0) {
                Glide.clear(viewHolder.imageView);
                Glide.with(mContext).load("file://" + mContext.getExternalFilesDir(null).getAbsolutePath() + "/" + "PlayCamera" + "/" + list.get(position).getHCGPaperID() + ".jpg").signature(new StringSignature("1")).into(viewHolder.imageView);
            }
        }
        return convertView;
    }

    private static class ViewHolder {
        TextView textViewLeft;
        ImageView imageView;
        TextView textViewRight;
    }
}
